package com.linkage.jz.bankruptcy.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.bean.ManagerUserInfo;
import com.linkage.seapeak.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import cn.bingoogolapple.swipeitemlayout.BGASwipeItemLayout;
import jp.wasabeef.glide.transformations.CropCircleTransformation;


public class ProOtherUserListViewAdapter extends BGARecyclerViewAdapter<ManagerUserInfo> {

    private boolean openOtherList = false;
    private boolean addOtherMembers = false;
    private boolean deleteOtherMembers = false;

    public void setAuthParms(boolean openOtherList, boolean addOtherMembers, boolean deleteOtherMembers) {
        this.openOtherList = openOtherList;
        this.addOtherMembers = addOtherMembers;
        this.deleteOtherMembers = deleteOtherMembers;
    }

    /**
     * 当前处于打开状态的item
     */
    private List<BGASwipeItemLayout> mOpenedSil = new ArrayList<>();
    private Context mContext;

    public ProOtherUserListViewAdapter(RecyclerView recyclerView,Context context) {
        super(recyclerView, R.layout.item_managerlist);
        mContext = context;
    }

    @Override
    public void setItemChildListener(BGAViewHolderHelper viewHolderHelper, int viewType) {
        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
        swipeItemLayout.setDelegate(new BGASwipeItemLayout.BGASwipeItemLayoutDelegate() {
            @Override
            public void onBGASwipeItemLayoutOpened(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
                mOpenedSil.add(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutClosed(BGASwipeItemLayout swipeItemLayout) {
                mOpenedSil.remove(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutStartOpen(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
            }
        });
        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_delete);
        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_edit);
        viewHolderHelper.setItemChildClickListener(R.id.iv_manageuser_add);
        viewHolderHelper.setItemChildClickListener(R.id.iv_email);
        viewHolderHelper.setItemChildClickListener(R.id.iv_phone);

        viewHolderHelper.setItemChildLongClickListener(R.id.tv_item_bgaswipe_delete);
    }

    @Override
    public void fillData(BGAViewHolderHelper viewHolderHelper, int position, ManagerUserInfo userInfo) {
//        viewHolderHelper.setText(R.id.tv_item_bgaswipe_title, "111")
//                .setText(R.id.tv_item_bgaswipe_detail,"22");

        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
        if ( userInfo.getCurType()== 1) {
            ImageView imageview = viewHolderHelper.getImageView(R.id.iv_item_bgaswipe_avatar1);
            Glide.with(mContext).load(userInfo.getUser().getAvatar())
                    .bitmapTransform(new CropCircleTransformation(mContext))
                    .placeholder(R.drawable.logo)
                    .into(imageview);

            viewHolderHelper.setVisibility(R.id.test, View.GONE);
            viewHolderHelper.setVisibility(R.id.test2,View.VISIBLE);
            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_edit, View.GONE);
            viewHolderHelper.setVisibility(R.id.iv_phone, userInfo.isHidePhone() ? View.GONE : View.VISIBLE);
            
            viewHolderHelper.setText(R.id.tv_item_bgaswipe_title1, userInfo.getName());
            String code = userInfo.getGroup().getCode();
            if (TextUtils.isEmpty(userInfo.getUser().getCompany()) && !code.equals("contributor_group"))
                viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_title2, View.GONE);
            else if (code.equals("contributor_group")){
                viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_title2, View.VISIBLE);
                viewHolderHelper.setText(R.id.tv_item_bgaswipe_title2, "出资金额：" + CommonUtils.resetMoney(userInfo.getContributiveMoney()) + "元");
            } else {
                viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_title2, View.VISIBLE);
                viewHolderHelper.setText(R.id.tv_item_bgaswipe_title2, userInfo.getSubsidiaryOrgan());
            }

            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_delete, deleteOtherMembers ? View.VISIBLE : View.GONE);
            swipeItemLayout.setSwipeAble(true);
        }else{
            viewHolderHelper.setVisibility(R.id.test2, View.GONE);
            viewHolderHelper.setVisibility(R.id.test,View.VISIBLE);
            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_edit, View.GONE);
            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_delete, userInfo.getGroupnum() == 0 ? View.VISIBLE : View.GONE);
            viewHolderHelper.setVisibility(R.id.iv_manageuser_add, addOtherMembers ? View.VISIBLE : View.GONE);
            viewHolderHelper.setText(R.id.tv_item_bgaswipe_title, userInfo.getGroup().getName()+"（"+userInfo.getGroupnum()+"）");
            swipeItemLayout.setSwipeAble(false);
        }
//            swipeItemLayout.setSwipeAble(false);
//        } else {

//        }
    }

    public void closeOpenedSwipeItemLayoutWithAnim() {
        for (BGASwipeItemLayout sil : mOpenedSil) {
            sil.closeWithAnim();
        }
        mOpenedSil.clear();
    }
}